package com.dzt.androidframework.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.dzt.androidframework.R;
import com.dzt.androidframework.utils.DialogMaker;
import com.dzt.androidframework.utils.JLogKit;
import com.dzt.androidframework.utils.JToastKit;
import com.dzt.androidframework.widgets.TitleView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by M02323 on 2017/11/20.
 * 带有自定义Title的Activity
 */

public abstract class ActivityTitle extends ActivityBase implements
		DialogMaker.DialogCallBack{
	protected Dialog dialog;
	protected ViewGroup rootView;
	protected View subView;
	@ViewInject(R.id.tv_title)
	protected TitleView titleView;
	protected LayoutInflater inflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		inflater = LayoutInflater.from(context);
		setContentView(getLayoutId());
		x.view().inject(this);
		titleView.setResource(R.drawable.selector_return_btn,
				R.string.menu_1);
		titleView.setLeftClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		initParams();
	}

	@Override
	public void setContentView(@LayoutRes int layoutResID) {
		super.setContentView(layoutResID);
		rootView = (ViewGroup) inflater.inflate(R.layout.activity_title, null, false);
		subView = inflater.inflate(layoutResID, null, false);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		subView.setLayoutParams(params);
		RelativeLayout mContainer = (RelativeLayout) rootView.findViewById(R.id.container);
		mContainer.addView(subView);
		getWindow().setContentView(rootView);
	}

	protected void setTitleView(int resId){
		titleView.setCenterDetail(resId);
	}

	protected void setTitleView(String title){
		titleView.setCenterDetail(title);
	}

	protected abstract int getLayoutId();
	protected abstract void initParams();

	@Override
	public void onBtnClicked(Dialog dialog, int position, Object tag) {
		JLogKit.getInstance().i("position = " + position);
	}

	/**
	 * Pop-up dialog
	 *
	 * @param title                  标题
	 * @param msg                    内容
	 * @param btns                   按钮数组
	 * @param isCanCancel            是否可以取消
	 * @param isDismissAfterClickBtn 点击后是否关闭提示框
	 * @return
	 */
	public Dialog showAlertDialog(String title, String msg, String[] btns,
								  boolean isCanCancel, final boolean isDismissAfterClickBtn,
								  Object tag) {
		if (null == dialog || !dialog.isShowing()) {
			dialog = DialogMaker.showCommonAlertDialog(this, title, msg, btns, this,
					isCanCancel, isDismissAfterClickBtn, tag);
		}
		return dialog;
	}

	public Dialog showWaitDialog(String msg, boolean isCanCancel, Object tag) {
		if (null == dialog || !dialog.isShowing()) {
			dialog = DialogMaker.showCommonWaitDialog(this, msg, this,
					isCanCancel, tag);
		}
		return dialog;
	}

	/**
	 * close dialog
	 */
	public void dismissDialog() {
		if (null != dialog && dialog.isShowing()) {
			dialog.dismiss();
		}
	}

	protected void showToast(String msg) {
		JToastKit.getInstance().showMessage(msg);
	}

	@Override
	protected void onDestroy() {
		dismissDialog();
		super.onDestroy();
	}
}
